package com.anjoyo.xgh.musicshare.adapter;

import java.io.File;

import com.anjoyo.xgh.musicshare.R;
import com.anjoyo.xgh.musicshare.activity.LoginAct;
import com.anjoyo.xgh.musicshare.activity.QRAct;
import com.anjoyo.xgh.musicshare.pageview.HomePage;
import com.anjoyo.xgh.musicshare.pageview.NetSongPage;
import com.anjoyo.xgh.musicshare.util.Constant;
import com.anjoyo.xgh.musicshare.util.MusicSpf;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import m.framework.ui.widget.slidingmenu.MenuAdapter;
import m.framework.ui.widget.slidingmenu.SlidingMenu;
import m.framework.ui.widget.slidingmenu.SlidingMenuItem;

public class SlidingAdapter extends MenuAdapter {
	public static final int GROUP_INFO=0;
	public static final int GROUP_CHOOSE=1;
	public static final int ITEM_NICKNAME=1;
	public static final int ITEM_HOME=2;
	public static final int ITEM_NETSONG=3;
	public static final int ITEM_LOGINOUT=4;
	public static final int ITEM_SET=5;
	public HomePage homepage;
	Context con;
	SlidingMenu menu;
	Activity activity;
	LayoutInflater inflater;
	public SlidingAdapter(SlidingMenu menu,Activity activity) {
		super(menu);
		con=menu.getContext();
		inflater = LayoutInflater.from(con);
		this.menu=menu;
		this.activity = activity;
		initdata();
	}

	
	public void initdata(){
		setGroup(GROUP_INFO, "用户信息");
		setGroup(GROUP_CHOOSE, "选择");
		
	 
		SlidingMenuItem menuItem=new SlidingMenuItem();
		menuItem.id=ITEM_NICKNAME;
		menuItem.body=MusicSpf.getloginUser(con).getNickname();
		setItem(GROUP_INFO, menuItem);
		
		SlidingMenuItem menuItem2=new SlidingMenuItem();
		menuItem2.id=ITEM_HOME;
		menuItem2.body="首页";
		setItem(GROUP_CHOOSE, menuItem2);
		SlidingMenuItem menuItem3=new SlidingMenuItem();
		menuItem3.id=ITEM_NETSONG;
		menuItem3.body="在线音乐";
		setItem(GROUP_CHOOSE, menuItem3);
		SlidingMenuItem item1=new SlidingMenuItem();
		item1.id=ITEM_SET;
		item1.body="设置";
		setItem(GROUP_CHOOSE, item1);
		SlidingMenuItem item2=new SlidingMenuItem();
		item2.id=ITEM_LOGINOUT;
		item2.body="注销登录";
		setItem(GROUP_CHOOSE, item2);
	}
	
	@Override
	public View getGroupView(int position, ViewGroup arg1) {
		TextView tv=new TextView(con);
		tv.setBackgroundResource(R.drawable.sidebar_titlt_back);
		int dp_13 = dipToPx(menu.getContext(), 13);
		int dp_5 = dipToPx(menu.getContext(), 3);
		tv.setPadding(dp_13, dp_5, dp_13, dp_5);
		tv.setText(getTitle(position));
		tv.setGravity(Gravity.CENTER_VERTICAL);
		tv.setTextColor(0xff999999);
		tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
		return tv;
	}

	@Override
	public View getItemView(SlidingMenuItem item, ViewGroup arg1) {
		if (item.id==ITEM_NICKNAME) {
			View v = inflater.inflate(R.layout.userinfo_layout, null);
			ImageView imageView = (ImageView) v.findViewById(R.id.iv_head);
			File file = new File(Constant.CACHE + "head.jpg");
			imageView.setImageURI(Uri.fromFile(file));
			TextView tv=(TextView) v.findViewById(R.id.tv_nickname);
			tv.setText(String.valueOf(item.body));
			return v;
		}else {
			TextView tv=new TextView(con);
			tv.setGravity(Gravity.CENTER_VERTICAL);
			tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 22);
			tv.setTextColor(tv.getResources().getColorStateList(
					R.color.normal_gray_pressed_white));
			tv.setText(String.valueOf(item.body));
			tv.setCompoundDrawablesWithIntrinsicBounds(
					0, 0, R.drawable.pointer, 0);
			int dp_13 = dipToPx(menu.getContext(), 13);
			int dp_52 = dipToPx(menu.getContext(), 52);
			tv.setCompoundDrawablePadding(dp_13);
			tv.setPadding(dp_13, 0, dp_13, 0);
			tv.setHeight(dp_52);
			return tv;
		}
	}
	private int dipToPx(Context context, int dip) {
		return (int) (dip * context.getResources().getDisplayMetrics().density + 0.5f);
	}

	@Override
	public boolean onItemTrigger(SlidingMenuItem item) {
		switch (item.id) {
		case ITEM_HOME:
			homepage=new HomePage(menu);
			menu.setBodyView(homepage.getpage());
			break;
		case ITEM_NETSONG:
			NetSongPage songpage=new NetSongPage(menu);
			menu.setBodyView(songpage.getpage());
			break;
		case ITEM_LOGINOUT:
			MusicSpf.clearloginInfo(menu.getContext());
			activity.finish();
			menu.getContext().startActivity(new Intent(menu.getContext(),LoginAct.class));
			break;

		case ITEM_SET:
			menu.getContext().startActivity(new Intent(menu.getContext(),QRAct.class));
			break;
		default:
			break;
		}
		return super.onItemTrigger(item);
	}
}
